package com.bao.DaoImpl;

import com.bao.Dao.AgencyDao;
import com.bao.Emtity.agency;
import com.jie.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 代理
 * Created by TangBaoshan on 2018/8/11.
 */
public class AgencyDaoImpl implements AgencyDao
{
    @Override
    /*
    * 根据查询i的代理信息
    * */
    public agency FindNumAgency(int id) {
        agency a = new agency();
        String sql = "SELECT * FROM agency WHERE  uid=? ";
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        conn = BaseDao.getConnection();
        try {
            stat=conn.prepareStatement(sql);
            stat.setInt(1, id);
            rs = stat.executeQuery();
            while (rs.next()) {
                a.setAgencyid(rs.getInt("agencyid"));
                a.setUid(rs.getInt("uid"));
                a.setAffiliating(rs.getInt("affiliating"));
                a.setDrank(rs.getInt("drank"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
      }
      return a;
    }
    @Override
    /*
    * 根据
    * */
    public List<agency> findregionName(String Name) {
        List<agency> list = new ArrayList<agency>();
        String sql = "SELECT * FROM agency WHERE  uid=? ";
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setString(1, Name);
            rs = stat.executeQuery();
            while (rs.next()) {
                agency a = new agency();
                a.setUid(rs.getInt("uid"));
                a.setAgencyid(rs.getInt("agencyid"));
                a.setAffiliating(rs.getInt("affiliating"));
                a.setDrank(rs.getInt("drank"));
                list.add(a);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
        }
        return list;
    }

    @Override
    /**
     *新增代理用户信息
     */
    public int Addagency(agency a) {
        String sql = "INSERT INTO `agency`  VALUES (null,?,?,?);";
        Object[] ob = {
                a.getUid(), a.getDrank(), a.getAffiliating()
        };
        return BaseDao.executeUpdate(sql,ob);
    }
    @Override
    public int findregionNum(int drank, int city) {
        String sql = "SELECT COUNT(1) FROM agency WHERE drank=? AND affiliating=?";
        Connection conn = null;
        PreparedStatement stat = null;
        ResultSet rs = null;
        int num = 0;
        conn = BaseDao.getConnection();
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1, drank);
            stat.setInt(2, city);
            rs = stat.executeQuery();
            while (rs.next()) {
                num = rs.getInt(1);
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            BaseDao.closeConnection(conn, stat, rs);
        }
        return num;
    }
}
